##################################
### Replication Alliance Building Corona
##################################

library(tidyverse) # for plotting, pipe etc.
library(rvest) # repair_enconding because qualtrics csv destroys german encoding
library(cjoint) # for reading in data
library(cregg) # for analyzing data (AMCE, MM etc.)
library(grid)
library("ggplotify")

cj_vac <- readRDS("alliance_data.rds")

##############################################
# Estimate main results (MMs)
##############################################

mm_formula <- as.formula("selected ~ factor_members + factor_eu + 
factor_distribution_costs + factor_distribution_units +
factor_units + factor_costs + factor_origin + factor_producer")

feature_labels <- list("factor_members" = "Members of the alliance\nare Germany and ...",
                       "factor_eu" = "The other members of\nthe alliance are ...",
                       "factor_distribution_costs" = "Distribution of costs\nwithin the alliance ...",
                       "factor_distribution_units" = "Distribution of the vaccine\n within the alliance ...",
                       "factor_units" = "Vaccine doses\nfor Germany ...",
                       "factor_costs" = "One-off costs\nper household in Germany ...",
                       "factor_origin" = "Vaccine manufacturer\ncomes from ...",
                       "factor_producer" = "Vaccine is\nproduced by ...")

vac_results <- cj(cj_vac,
                  mm_formula,
                  estimate = "mm",
                  id = ~ new_id,
                  feature_labels = feature_labels)

# Plot main results

ggplot(vac_results, 
       aes(x = level, 
           y = estimate,
           color = feature)) +
  coord_flip() +
  geom_hline(yintercept = 0.5, lty = 2) +
  geom_point(size = 1.5, 
             position = position_dodge(width = 0.5)) +
  geom_errorbar(aes(ymax = lower, 
                    ymin = upper, 
                    width = 0),
                position = position_dodge(width = 0.5)) +
  theme_bw() +
  facet_grid(feature ~ ., 
             scales  ="free", 
             space = "free",
             switch = "y") +
  scale_x_discrete(position="top") +
  theme(plot.background = element_rect(fill = 'white'),
        axis.text.y = element_text(size=10.5, 
                                   color = "black"),
        axis.text.x = element_text(angle = 0,
                                   size=10.5, 
                                   color = "black"),
        strip.text.y = element_text(angle = 90,
                                    color = "black",
                                    size = 10.5),
        strip.text.y.left = element_text(angle = 0),
        strip.background = element_rect(fill = 'white')) +
  theme(plot.margin=unit(c(1,1,1,3),
                         "line"),
        panel.border = element_rect(colour = "black"),
        legend.position = "bottom") +
  xlab("") +
  ylab("Marginal Means") +
  guides(color = FALSE) -> plot_vaccine

gt <- ggplot_gtable(ggplot_build(plot_vaccine))
gt$heights[1] = 2 * gt$heights[1]
gt$heights[6] = 5 * gt$heights[6]
gt$heights[10] = 5 * gt$heights[10]
gt$heights[14] = 5 * gt$heights[14]
gt$heights[18] = 5 * gt$heights[18]

as.ggplot(gt) -> vacc_plot

x_pos <- 0.06
font_size = 6

vacc_plot + 
  coord_cartesian(clip = "off") + 
  annotate("text", x = x_pos, y = .965, 
           hjust = 0, vjust = 0,
           size = font_size, 
           color = "black", 
           label = "Alliance composition") + 
  annotate("text", x = x_pos, y = .755, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Alliance distribution rules") + 
  annotate("text", x = x_pos, y = .55, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine nationalism") + 
  annotate("text", x = x_pos, y = .29, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine confidence") -> vacc_plot_final

vacc_plot_final

ggsave(vacc_plot_final,
       file = "plot_vaccine_mm.jpg",
       width = 10,
       height = 8.5,
       dpi = 666)

##############################################
# Conditional on Therat
#############################################

# MM

vac_results_threat_mm <- cj(cj_vac,
                            mm_formula,
                            estimate = "mm",
                            by = ~ threat,
                            id = ~ new_id,
                            feature_labels = feature_labels)

vac_results_threat_mm$threat <- factor(vac_results_threat_mm$threat, 
                                       levels = paste0(c("Low", "Moderate", "High"), 
                                                       " Threat"))

vac_results_threat_mm$model <- "Marginal Means"

# Diff in MM

vac_results_threat_diff_mm <- cj(cj_vac,
                                 mm_formula,
                                 estimate = "mm_diff",
                                 by = ~ threat,
                                 id = ~ new_id,
                                 feature_labels = feature_labels)

vac_results_threat_diff_mm$threat <- factor(vac_results_threat_diff_mm$threat, 
                                            levels = paste0(c("Low", "Moderate", "High"), 
                                                            " Threat"))

vac_results_threat_diff_mm$model <- "Difference in\nMarginal Means"

# Combine

vac_results_threat <- bind_rows(vac_results_threat_mm,
                                vac_results_threat_diff_mm)

vac_results_threat$model <- factor(vac_results_threat$model,
                                   levels = unique(vac_results_threat$model))

yline_df <- data.frame(model = unique(vac_results_threat$model),
                       intercept = c(0.5,0))

# Plot combined Results

ggplot(vac_results_threat, 
       aes(x = level, 
           y = estimate,
           color = threat,
           shape = threat)) +
  coord_flip() +
  geom_hline(data = yline_df, 
             aes(yintercept = intercept), lty = 2) +
  geom_point(size = 1.5, 
             position = position_dodge(width = 0.7)) +
  geom_errorbar(aes(ymax = lower, 
                    ymin = upper, 
                    width = 0),
                position = position_dodge(width = 0.7)) +
  theme_bw() +
  facet_grid(feature ~ model, 
             scales  ="free", 
             space = "free",
             switch = "y") +
  scale_x_discrete(position="top") +
  theme(plot.background = element_rect(fill = 'white'),
        axis.text.y = element_text(size=10.5, 
                                   color = "black"),
        axis.text.x = element_text(angle = 0,
                                   size=10.5, 
                                   color = "black"),
        strip.text.y = element_text(angle = 90,
                                    color = "black",
                                    size = 10.5),
        strip.text.y.left = element_text(angle = 0),
        strip.background = element_rect(fill = 'white')) +
  theme(plot.margin=unit(c(1,1,1,3),
                         "line"),
        panel.border = element_rect(colour = "black"),
        legend.position = "bottom") +
  scale_color_manual(values = c("purple", "steelblue", "orange")) +
  scale_shape_manual(values = c(1, 16, 17)) +
  labs(color = "Perceived Threat\nof COVID-19 Pandemic",
       shape = "Perceived Threat\nof COVID-19 Pandemic",
       x = "",
       y = "") -> plot_vaccine_mm

plot_vaccine_mm

gt <- ggplot_gtable(ggplot_build(plot_vaccine_mm))
gt$heights[11] = 4 * gt$heights[11]
gt$heights[15] = 4 * gt$heights[15]
gt$heights[19] = 4 * gt$heights[19]

as.ggplot(gt) -> vacc_plot

vacc_plot

x_pos <- 0.05
font_size = 5

vacc_plot + 
  coord_cartesian(clip = "off") + 
  annotate("text", x = x_pos, y = .94, 
           hjust = 0, vjust = 0,
           size = font_size, 
           color = "black", 
           label = "Alliance composition") + 
  annotate("text", x = x_pos, y = .755, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Alliance distribution rules") + 
  annotate("text", x = x_pos, y = .57, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine nationalism") + 
  annotate("text", x = x_pos, y = .34, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine confidence") -> vacc_plot_final

vacc_plot_final

ggsave(vacc_plot_final,
       file = "plot_vaccine_by_threat.jpg",
       width = 12,
       height = 9,
       dpi = 666)

##############################################
# Conditional on LR
#############################################

# MM

vac_results_threat_mm <- cj(cj_vac,
                            mm_formula,
                            estimate = "mm",
                            by = ~ lr3,
                            id = ~ new_id,
                            feature_labels = feature_labels)

vac_results_threat_mm$lr3 <- factor(vac_results_threat_mm$lr3, 
                                    levels = paste0(c("Left", "Center", "Right")))

vac_results_threat_mm$model <- "Marginal Means"

# Diff in MM

vac_results_threat_diff_mm <- cj(cj_vac,
                                 mm_formula,
                                 estimate = "mm_diff",
                                 by = ~ lr3,
                                 id = ~ new_id,
                                 feature_labels = feature_labels)

vac_results_threat_diff_mm$lr3 <- factor(vac_results_threat_diff_mm$lr3, 
                                         levels = paste0(c("Left","Center","Right")))

vac_results_threat_diff_mm$model <- "Difference in\nMarginal Means"

# Combine

vac_results_threat <- bind_rows(vac_results_threat_mm,
                                vac_results_threat_diff_mm)

vac_results_threat$model <- factor(vac_results_threat$model,
                                   levels = unique(vac_results_threat$model))

yline_df <- data.frame(model = unique(vac_results_threat$model),
                       intercept = c(0.5,0))

# Plot combined Results

ggplot(vac_results_threat, 
       aes(x = level, 
           y = estimate,
           color = lr3,
           shape = lr3)) +
  coord_flip() +
  geom_hline(data = yline_df, 
             aes(yintercept = intercept), lty = 2) +
  geom_point(size = 1.5, 
             position = position_dodge(width = 0.7)) +
  geom_errorbar(aes(ymax = lower, 
                    ymin = upper, 
                    width = 0),
                position = position_dodge(width = 0.7)) +
  theme_bw() +
  facet_grid(feature ~ model, 
             scales  ="free", 
             space = "free",
             switch = "y") +
  scale_x_discrete(position="top") +
  theme(plot.background = element_rect(fill = 'white'),
        axis.text.y = element_text(size=10.5, 
                                   color = "black"),
        axis.text.x = element_text(angle = 0,
                                   size=10.5, 
                                   color = "black"),
        strip.text.y = element_text(angle = 90,
                                    color = "black",
                                    size = 10.5),
        strip.text.y.left = element_text(angle = 0),
        strip.background = element_rect(fill = 'white')) +
  theme(plot.margin=unit(c(1,1,1,3),
                         "line"),
        panel.border = element_rect(colour = "black"),
        legend.position = "bottom") +
  scale_color_manual(values = c("purple", "steelblue", "orange")) +
  scale_shape_manual(values = c(1, 16, 17)) +
  labs(color = "Ideology",
       shape = "Ideology",
       x = "",
       y = "") -> plot_vaccine_mm

plot_vaccine_mm

gt <- ggplot_gtable(ggplot_build(plot_vaccine_mm))
gt$heights[11] = 4 * gt$heights[11]
gt$heights[15] = 4 * gt$heights[15]
gt$heights[19] = 4 * gt$heights[19]

as.ggplot(gt) -> vacc_plot

vacc_plot

x_pos <- 0.05
font_size = 5

vacc_plot + 
  coord_cartesian(clip = "off") + 
  annotate("text", x = x_pos, y = .94, 
           hjust = 0, vjust = 0,
           size = font_size, 
           color = "black", 
           label = "Alliance composition") + 
  annotate("text", x = x_pos, y = .755, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Alliance distribution rules") + 
  annotate("text", x = x_pos, y = .57, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine nationalism") + 
  annotate("text", x = x_pos, y = .34, 
           vjust = 0, hjust = 0, 
           size = font_size, 
           color = "black", 
           label = "Vaccine confidence") -> vacc_plot_final

vacc_plot_final

ggsave(vacc_plot_final,
       file = "plot_vaccine_by_lr.jpg",
       width = 12,
       height = 9,
       dpi = 666)


